<?php
/**
 * Created by PhpStorm.
 * User: ari
 * Date: 11/17/14
 * Time: 12:34 PM
 */
namespace BC\User;

use CPath\Data\Describable\IDescribable;
use CPath\Render\HTML\Attribute\IAttributes;
use CPath\Render\HTML\Element\HTMLElement;
use CPath\Render\HTML\Element\HTMLTable;
use CPath\Render\HTML\Header\HTMLHeaderScript;
use CPath\Render\HTML\Header\HTMLHeaderStyleSheet;
use CPath\Render\HTML\RenderCallback;

class UserUIContainer extends HTMLElement
{
	const CSS_CLASS = 'user-ui-container';
	const CSS_ITEM_CLASS = 'container-item';
	const CSS_ITEM_CONTENT_CLASS = 'container-item-content';

	/**
	 * @param String|Array|IAttributes $attributes attribute html as string, array, or IAttributes instance
	 * @param null $_content
	 * @internal param bool $closed
	 */
	public function __construct($attributes = null, $_content = null) {
		parent::__construct('div', $attributes);
		$this->addAll(array_slice(func_get_args(), 1));
		$this->addSupportHeaders(new HTMLHeaderScript(__DIR__ . '/assets/user-ui-container.js'));
		$this->addSupportHeaders(new HTMLHeaderStyleSheet(__DIR__ . '/assets/user-ui-container.css'));

		$ItemTemplate = new HTMLElement('div', self::CSS_ITEM_CLASS,
			$Legend = new HTMLElement('legend'),
			$Content = new HTMLElement('div', self::CSS_ITEM_CONTENT_CLASS)
		);
		$ItemTemplate->setContainer($Content);
		$Legend->addContent(
			new RenderCallback(
				function() use ($Content) {
					$Renders = $Content->getContent();
					$Render = reset($Renders);
					$key = key($Renders);
					if($Render instanceof IDescribable) {
						echo $Render->getDescription();
					} elseif(is_string($key)) {
						echo ucwords(str_replace('_', ' ', $key));
					} else {
						echo get_class($Render);
					}
				}
			)
		);
		$this->setItemTemplate($ItemTemplate);
		$ItemTemplate->addContent(new HTMLTable());
	}

}